﻿// See https://aka.ms/new-console-template for more information
using Flee.CalcEngine.PublicTypes;
using Flee.PublicTypes;

// 简单计算
var formulaString = "2*(3+5)-3.14";
var context = new ExpressionContext();
context.Imports.AddType(typeof(Math));

var result = context.CompileDynamic(formulaString);
Console.WriteLine($"{formulaString}={result.Evaluate()}");

// 变量计算
formulaString = "a+b*3.14+sqrt(c)";
context.Variables["a"] = 10;
context.Variables["b"] = 100;
context.Variables["c"] = 5;
result = context.CompileDynamic(formulaString);
var result1 = context.CompileGeneric<double>(formulaString);
Console.WriteLine($"{formulaString}={result.Evaluate()} {result1.Evaluate()}");

// 多表达式组合
var engine = new CalculationEngine();
engine.Add("X", formulaString, context);

// 定义表达式Y
var formulaString1 = "a1+10";
context.Variables["a1"] = 20;
engine.Add("Y", formulaString1, context);

// 定义组合表达式
engine.Add("Z", "X+Y", context);
Console.WriteLine($"{engine.GetExpression("Z")}={engine.GetResult("Z")}");